package fr.enseirb.collabs.persistence;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import fr.enseirb.collabs.model.Collaborator;
import fr.enseirb.collabs.model.Profile;
import fr.enseirb.collabs.model.Skill;

public class MemoryDataBundle {

	private static MemoryDataBundle _INSTANCE;

	private int _COLLAB_ID_SEQ = 1;
	private int _SKILL_ID_SEQ = 1;

	private Map<Integer, Collaborator> COLLABS;
	private Map<Integer, Skill> SKILLS;

	private MemoryDataBundle() {
		COLLABS = new HashMap<Integer, Collaborator>();
		SKILLS = new HashMap<Integer, Skill>();

		initMockData();
	}

	public static final MemoryDataBundle getInstance() {
		if (_INSTANCE == null) {
			_INSTANCE = new MemoryDataBundle();
		}
		return _INSTANCE;
	}

	private void initMockData() {

		// Skills
		Skill s1 = new Skill("Java/JEE", "Java/JEE Skills", null);
		Skill s2 = new Skill(".NET", ".NET Skills", null);
		Skill s3 = new Skill("Python", "Python Skills", null);
		Skill s4 = new Skill("Ruby", "Ruby Skills", null);

		int s1Id = addSkill(s1);
		int s2Id = addSkill(s2);
		addSkill(s3);
		addSkill(s4);

		// SubSkills
		Skill ss1 = new Skill("Spring", "Spring Skills", s1Id);
		Skill ss2 = new Skill("Hibernate", "Hibernate Skills", s1Id);
		Skill ss3 = new Skill("ASP", "ASP Skills", s2Id);
		addSkill(ss1);
		addSkill(ss2);
		addSkill(ss3);

		// Collabs
		Collaborator collab1 = new Collaborator(new Profile("ORIOU", "Elian",
				"17 CHEMIN", "0102030405", "eoriou@toto.com", "Dev"));
		Collaborator collab2 = new Collaborator(new Profile("GALVAO",
				"Pauline", "Avenue de la pierre", "0102030405",
				"pgalvao@toto.com", "Dev"));
		addCollaborator(collab1);
		addCollaborator(collab2);

		// // Collabs => Skills
		collab1.addSkill(s1.getId());
		collab1.addSkill(ss1.getId());
		collab1.addSkill(ss2.getId());
		collab1.addSkill(s2.getId());
		collab2.addSkill(s2.getId());
		collab2.addSkill(s3.getId());
		collab2.addSkill(s4.getId());
		collab2.addSkill(ss3.getId());
	}

	// ///////////////////
	// READ METHODS
	// ///////////////////

	public Collection<Collaborator> getCollabs() {
		return COLLABS.values();
	}

	public Collaborator getCollabByID(int id) {
		return COLLABS.get(id);
	}

	public Collection<Skill> getSkills() {
		return SKILLS.values();
	}

	public Skill getSkillByID(int id) {
		return SKILLS.get(id);
	}

	// ///////////////////
	// ADD METHODS
	// ///////////////////

	public int addCollaborator(Collaborator c) {
		int newId = _COLLAB_ID_SEQ++;
		c.setId(newId);
		COLLABS.put(newId, c);
		return newId;
	}

	public int addSkill(Skill s) {
		int newId = _SKILL_ID_SEQ++;
		s.setId(newId);
		SKILLS.put(newId, s);
		return newId;
	}

	// ///////////////////
	// DELETE METHODS
	// ///////////////////

	public boolean deleteCollaborator(int id) {
		return COLLABS.remove(id) != null;
	}

	public boolean deleteSkill(int id) {
		return SKILLS.remove(id) != null;
	}
}
